//
//  MYNavigationController.m
//  奇闻
//
//  Created by 似水灵修 on 14-11-20.
//  Copyright (c) 2014年 MingYueGuCheng. All rights reserved.
//

#import "MYNavigationController.h"

@interface MYNavigationController ()<UINavigationControllerDelegate>
@property (nonatomic, strong) id interactiveDelegate;
@end

@implementation MYNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
}
+ (void)initialize
{
    //设置导航栏UINavigationBar主题
    [self setupNavigationBarTheme];
    //设置导航栏UIBarButtonItem主题样式
    [self setupBarButtonItemTheme];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 只要覆盖了导航条系统自带的左边按钮,这个代理就会做些事情
    // 实现滑动返回功能
    // 直接删除,系统的某些相当于不会调用
    self.interactiveDelegate = self.interactivePopGestureRecognizer.delegate;
    self.delegate = self;
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (viewController != [self.viewControllers firstObject]) {
        self.interactivePopGestureRecognizer.delegate = nil;
    }
    else {
        self.interactivePopGestureRecognizer.delegate = self.interactiveDelegate;
    }
}
+ (void)setupBarButtonItemTheme
{
    UIBarButtonItem *appearance = [UIBarButtonItem appearance];
    NSMutableDictionary *textAttr = [NSMutableDictionary dictionary];
    textAttr[NSFontAttributeName] = MYNavigationTitleFont;
    textAttr[NSForegroundColorAttributeName] = [UIColor orangeColor];
    [appearance setTitleTextAttributes:textAttr forState:UIControlStateNormal];
}
+ (void)setupNavigationBarTheme
{
    //设置导航栏主题
    UINavigationBar *appearance = [UINavigationBar appearance];
    NSMutableDictionary *textAttr = [NSMutableDictionary dictionary];
    textAttr[NSFontAttributeName] = MYNavigationTitleFont;
    textAttr[NSForegroundColorAttributeName] = [UIColor blackColor];
    [appearance setTitleTextAttributes:textAttr];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 0) {
        [self setupLeftRightBarButtonWithViewController:viewController];
    }
    [super pushViewController:viewController animated:animated];
}
//设置导航栏左右按钮
- (void)setupLeftRightBarButtonWithViewController:(UIViewController *)viewController
{
    viewController.hidesBottomBarWhenPushed = YES;
    viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithNormalImageName:@"navigationbar_back" highImageName:@"navigationbar_back_highlighted" target:self action:@selector(popPre)];
    viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithNormalImageName:@"navigationbar_more" highImageName:@"navigationbar_more_highlighted" target:self action:@selector(popRoot)];
}
- (void)popPre
{
    [self popViewControllerAnimated:YES];
}
- (void)popRoot
{
    [self popToRootViewControllerAnimated:YES];
}
@end
